<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ChangeStatus;
use App\Admin\Repositories\IntoIncubatedEnterprise;
use App\Models\HubList;
use App\Models\IntoIncubatedEnterprise as ModelsIntoIncubatedEnterprise;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class IntoIncubatedEnterpriseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new IntoIncubatedEnterprise(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('address');
            $grid->column('hub')->display( function ($v) {
                return (HubList::find($v))['name'];
            });
            $grid->column('cover')->image('', 120, 60);
            $grid->column('contact');
            $grid->column('phone');
            $grid->column('status')->using([0 => '停业', 1 => '开业'])
                ->label([0 => 'gray', 1 => 'green']);

            $grid->column('updated_at')->sortable();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->prepend(new ChangeStatus(ModelsIntoIncubatedEnterprise::class, '停业/开业'));
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new IntoIncubatedEnterprise(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('hub')->as(function ($v) {
                return (HubList::find($v))['name'];
            });
            $show->field('address');
            $show->field('contact');
            $show->field('phone');
            $show->field('content')->unescape();

            $show->field('cover')->image();
            $show->field('images')->image();
            $show->field('qualification')->image();
            $show->field('status')->using([0 => '停业', 1 => '开业'])->label();

            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new IntoIncubatedEnterprise(), function (Form $form) {

            $form->display('id');
            $form->text('name');
            $form->text('address');
            $form->select('hub')->options(HubList::all()->pluck('name', 'id'));
            $form->text('contact')->width(2);
            $form->mobile('phone')->options(['mask' => '999 9999 9999']);
            $form->editor('content');

            $form->image('cover')->autoUpload()->saveFullUrl()
                ->accept('jpg,png,gif,jpeg');
            $form->multipleImage('images')->autoUpload()->saveFullUrl()
                ->accept('jpg,png,gif,jpeg')
                ->saving(function ($v) {
                    return implode(',', $v);
                });;
            $form->multipleImage('qualification')->autoUpload()->saveFullUrl()
                ->accept('jpg,png,gif,jpeg')
                ->saving(function ($v) {
                    return implode(',', $v);
                });

            $form->radio('status')->options([0 => '停业', 1 => '开业'])->default(1);


            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
